﻿using Sys.Common;
using Sys.Core.Sys;
using Sys.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sys.Api.Sys
{
    public class MenuApi : BaseApi
    {
        public string Grid()
        {
            var entities = MenuBll.I.SortEntities;

            List<JsonMap> jms = new List<JsonMap>();

            jms.Add(new JsonMap("Icon", "iconCls"));

            return CustomJson(entities.ToList(), jms, true);
        }

        public string Tree()
        {
            var list = MenuBll.I.SortEntities.ToList();

            list.Insert(0, MenuBll.I.NullModel());

            List<JsonMap> jms = new List<JsonMap>();

            jms.Add(new JsonMap("ID", "id"));
            jms.Add(new JsonMap("Name", "text"));
            jms.Add(new JsonMap("Parent", "parent"));
            jms.Add(new JsonMap("Icon", "iconCls"));

            return CustomJson(list, jms, false);
        }

        public string Save(SysMenu model)
        {
            if (model.Parent.Equals("Null")) { model.Parent = string.Empty; }

            JsonMsg result = new JsonMsg();

            if (string.IsNullOrEmpty(model.ID))
            {
                result.Message = "添加成功!";

                model.ID = StringHelper.GetGuid();

                model.IntSort = MenuBll.I.MaxSort(m => m.IntSort);

                result.Success = MenuBll.I.Insert(model) > 0;
            }
            else
            {
                result.Message = "修改成功!";

                result.Success = MenuBll.I.Update(model) > 0;
            }

            if (!result.Success) { result.Message = "操作失败!"; }

            return JsonHelper.ToJson(result);
        }

        public string Delete(string id)
        {
            JsonMsg result = new JsonMsg();

            result.Success = MenuBll.I.Delete(id) > 0;

            if (result.Success) { result.Message = "删除成功!"; }

            return JsonHelper.ToJson(result);
        }

        public string SetOrder(string id, string parent, string order)
        {
            JsonMsg result = new JsonMsg();

            var entities = MenuBll.I.Entities.Where(m => m.Parent.Equals(parent));

            bool success = false;

            result.Message = MenuBll.I.SetOrder(entities, id, "IntSort", order, out success);

            result.Success = success;

            return JsonHelper.ToJson(result);
        }

        public string Assign(string id, string rows)
        {
            List<SysMenuButton> list = new List<SysMenuButton>();

            list = JsonHelper.Deserialize<List<SysMenuButton>>(rows);

            JsonMsg result = new JsonMsg();

            result.Success = MenuButtonBll.I.Assign(id, list);

            if (result.Success) { result.Message = "分配成功!"; }

            return JsonHelper.ToJson(result);
        }

        public string ButtonGrid(string id)
        {
            var entities = MenuButtonBll.I.EntitiesByMenuID(id);

            return JsonHelper.ToJson(entities.ToList());
        }

        public string EnableOrDisable(string id, bool bit)
        {
            JsonMsg result = new JsonMsg();

            SysMenu model = MenuBll.I.GetByKey(id);

            if (model != null)
            {
                if (model.IsVisible != bit)
                {
                    model.IsVisible = bit;

                    result.Success = MenuBll.I.Update(model) > 0;

                    if (result.Success)
                    {
                        result.Message = bit ? "启用成功!" : "禁用成功!";
                    }
                }
                else
                {
                    result.Message = bit ? "已经是启用状态!" : "已经是禁用状态!";
                }
            }

            return JsonHelper.ToJson(result);
        }

        public string RolePurviewGrid(string id)
        {
            List<JsonMap> jms = new List<JsonMap>();

            jms.Add(new JsonMap("Icon", "iconCls"));

            var menus = MenuBll.I.SortEntities.ToList();

            var mblist = MenuButtonBll.I.SortEntities.ToList();
            var blist = ButtonBll.I.SortEntities.ToList();
            var rmblist = RoleMenuButtonBll.I.EntitiesByRoleID(id).ToList();

            List<Hashtable> hlist = CustomHash(menus, jms, true);

            foreach (var model in blist)
            {
                foreach (Hashtable item in hlist)
                {
                    string value = "false"; string disabled = "true";

                    Hashtable node = new Hashtable();

                    foreach (var mb in mblist)
                    {
                        if (mb.MenuID == item["ID"].ToString() && mb.ButtonID.Equals(model.ID))
                        {
                            disabled = "false";
                            node["mbid"] = mb.ID;
                        }
                    }

                    foreach (var rmb in rmblist)
                    {
                        if (rmb.SysMenuButton.MenuID == item["ID"].ToString() && rmb.SysMenuButton.ButtonID.Equals(model.ID))
                        {
                            value = "true"; break;
                        }
                    }

                    node["value"] = value;
                    node["disabled"] = disabled;

                    item[model.Function] = node;
                }
            }

            return JsonHelper.ToJson(hlist);
        }

        public string UserPurviewGrid(string id)
        {
            List<JsonMap> jms = new List<JsonMap>();

            jms.Add(new JsonMap("Icon", "iconCls"));

            var menus = MenuBll.I.SortEntities.ToList();

            var mblist = MenuButtonBll.I.SortEntities.ToList();
            var blist = ButtonBll.I.SortEntities.ToList();
            var umblist = UserMenuButtonBll.I.EntitiesByUserID(id).ToList();

            List<Hashtable> hlist = CustomHash(menus, jms, true);

            foreach (var model in blist)
            {
                foreach (Hashtable item in hlist)
                {
                    string value = "false"; string disabled = "true";

                    Hashtable node = new Hashtable();

                    foreach (var mb in mblist)
                    {
                        if (mb.MenuID == item["ID"].ToString() && mb.ButtonID.Equals(model.ID))
                        {
                            disabled = "false";
                            node["mbid"] = mb.ID;
                        }
                    }

                    foreach (var umb in umblist)
                    {
                        if (umb.SysMenuButton.MenuID == item["ID"].ToString() && umb.SysMenuButton.ButtonID.Equals(model.ID))
                        {
                            value = "true"; break;
                        }
                    }

                    node["value"] = value;
                    node["disabled"] = disabled;

                    item[model.Function] = node;
                }
            }

            return JsonHelper.ToJson(hlist);
        }
    }
}
